(function(window){
    var arr=[],
        push=arr.push,
        slice=arr.slice
    function Itcast(selector){
        return new Itcast.fn.init(selector);
    }
    Itcast.fn=Itcast.prototype={
        constructor:Itcast,
        lenght:0,
        init:function(selector){
            if(!selector) return this;
            if(typeof selector==="string"){
                if(/^\s*</.test(selector)){
                    push.apply(this,Itcast.parseHTML(selector));
                }else{
                    push.apply(this,Itcast.select(selector))
                }
                return this;
            }
        }
    };
    Itcast.fn.init.prototype=Itcast.fn;
    Itcast.extend=Itcast.fn.extend=function(obj){
        for(k in obj){
            this[k]=obj[k];
        }
        return this;
    };
    Itcast.extend({
        select:function(selector){
            return document.querySelectorAll(selector);
        },
        isArrayLike:function(arr){
            if(Object.prototype.toString.call(arr)==="[object Array]"){
                return true
            }
            var length="length" in arr&&arr.length;
            return typeof length=="number"&&length>=0;
        },
        each:function(arr,callback){
            if(isArrayLike(arr)){
                for(var i=0;i<arr.length;i++){
                    if(callback.call(arr[i],i,arr[i])===false){
                        break
                    }
                }
            }else{
                for(var k in arr){
                    if(callback.call(arr[k],k,arr[k])===false){
                        break
                    }
                }
            }
            return arr;
        },
        map:function(arr,callback){
            var newArr=[],tmp;
            if(isArrayLike(arr)){
                for(var i=0;i<arr.length;i++){
                    tmp=callback(arr[i],i);
                    if(tmp!=null){
                        newArr.push(tmp)
                    }
                }
            }else{
                for(var k in arr){
                    tmp=callback(arr[k],k);
                    if(tmp!=null){
                        newArr.push(tmp)
                    }
                }
            }
            return newArr;
        }
    })
    Itcast.fn.extend({
        each:function(callback){
            return Itcast.each(this,callback);
        },
        map:function(callback){
            return Itcast.map(this,callback);
        },
        toArray:function(){
            return slice.call(this);
        },
        get:function(index){
            if(index===undefined){
                return this.toArray()
            };
            if(index>0){
                return this[this.length+index];
            }else{
                return this[index]
            }
        },
        first:function(){
            return this.eq(0)
        },
        last:function(){
            return this.eq(-1)
        },
        eq:function(index){
            var iobj=this.constructor();
            if(index==null) return iobj
            var dom=this.get(index);
            if(dom){
                iobj[0]=dom;
                iobj.lenght=1;
            }
            return iobj;
        },
        appendTo:function(arr){
            var tmp=[],tmpNode;
            for(var j=0;j<this.length;j++){
                for(var i=0;i<this.length;i++){
                    tmpNode=i==arr.length-1?this[j]:this[j].cloneNode(true);
                    tmp.push(tmpNode);
                    arr[i].appendChild(tmpNode)
                }
            }
            return tmp
        }
    })
    window.Itcast=window.I=Itcast;
})(window)